package com.web_bloks.content.client;

import com.google.gwt.user.client.HistoryListener;
import com.web_bloks.system.client.Logger;
import com.web_bloks.url.client.Helper;
import com.web_bloks.url.client.UrlFields;

/**
 * Pages:
 * - local-based  (i.e. packaged with the application)
 * - module-based (i.e. separate gwt-module) 
 * - server-based (i.e. serves through an Web-API)
 * @deprecated
 */
public class HistoryManager implements HistoryListener {

	ContentManager cm = null;
	
	String defaultPageName = null;
	
	protected HistoryManager() {	
	}
	/*==============================================
	  CONFIGURATION
	*/
	public void setContentManager(ContentManager c) {
		cm = c;
	}
	public void setDefaultPage(String pageName) {
		defaultPageName = new String (pageName);
	}
	/*==============================================
	  CALLBACKS
	*/
	public void onHistoryChanged(String historyToken) {
		navigateTo( historyToken );
	}
	/*==============================================
	  API
	*/	
	public void navigateTo(UrlFields uf) {
		
		if (uf == null) {
			navigateDefault();
			return;
		}
		if (uf.page == null || uf.page.length()==0) {
			navigateDefault();
			return;
		}
		
		cm.displayPage(uf);
	}
	/**
	 * This is the normal interface for navigation
	 * 
	 * @param token String
	 */
	public void navigateTo(String token) {
		
		Logger.logInfo(this.getClass()+".navigateTo: token["+token+"]");

		if (null==token || token.length()==0) {
			navigateDefault();
			return;
		}
		
		UrlFields uf = Helper.extractFieldsFromHistoryToken( token );
		
		if ( null==uf ) {
			navigateDefault();
			return;
		}
		
		Logger.logInfo(this.getClass()+".navigateTo: page["+uf.page+"]");
		
		cm.displayPage(uf);
	}

	/**
	 * Default page
	 */
	public void navigateDefault() {
		cm.displayPage( defaultPageName , null );
	}

}//END